package com.haoyu.number;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author 蒿雨
 * @create 2021-11-29 8:33
 * 二分查找法
 */
public class Test {
    public static void main(String[] args) {
//        //二叉树查找数组
//        Comparable[] arr = {15, 9456, 882, 686, 205, 771, 822};
//        System.out.println("输出该数组");
//        for (Comparable comparable : arr) {
//            System.out.printf("%d\t", comparable);
//        }
//        System.out.println();
//        int index = find(arr, 822);
//        System.out.printf("该数字的下标为%d", index);
        dl();
    }

    //队列
    public static void dl() {
        Queue<String> queue = new LinkedList<>();
        queue.offer("a");
        queue.offer("b");
        queue.offer("c");
        queue.offer("d");
        queue.offer("e");
        queue.offer("f");
        for (String s : queue) {
            System.out.print(s + "\t");
        }
        System.out.println();
        queue.poll();
        for (String s : queue) {
            System.out.print(s + "\t");
        }


    }


    // 二分查找法,在有序数组arr中,查找target
    // 如果找到target,返回相应的索引index
    // 如果没有找到target,返回-1
    public static int find(Comparable[] arr, Comparable target) {
        // 在arr[l...r]之中查找target
        //l:数组最小的下标
        //r:数组的最大下标

        int l = 0, r = arr.length - 1;
        while (l <= r) {
            //int mid = (l + r)/2;
            //mid:返回的数字所在位置
            // 防止极端情况下的整形溢出，使用下面的逻辑求出mid
            int mid = l + (r - l) / 2;

            if (arr[mid].compareTo(target) == 0) {
                return mid;
            }
            if (arr[mid].compareTo(target) > 0) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }

        return -1;
    }

    //递归
    public static void text(int a) {
        if (a-- > 2) {
            text(a);
        }
        System.out.print(a);
    }

    //递归算法1
    @org.junit.Test
    public void test01() {
        degui(5);
    }

    public void degui(int i) {
        System.out.println(i);
        if (i <= 5000) {
            degui(i * 2);
        }
    }

    //递归算法2
    @org.junit.Test
    public void test02() {
        int n = 0;
        int age = digui2(10, n);
        System.out.printf("第8个人%d岁", age);
    }

    public int digui2(int age, int n) {
        int i = 8;
        age += 2;
        if (i != ++n) {
            digui2(age, n);
        }
        if (i == n) {
            return age;
        }
        return 0;
    }


    @org.junit.Test
    public void test03() {
        System.out.println(computeAge(8));
    }

    public int computeAge(int n) {
        //当只有1人，返回10岁
        if (n == 1) {
            return 10;
        }
        //减法算法
        return computeAge(n - 1) + 2;
    }

    //金额单位
    @org.junit.Test
    public void money() {
        String str = sort(54321);
        System.out.println(str);
    }

    public String sort(Integer yuan) {
        String[] dw = {"万","千","百","十","元"};
        String[] money = yuan.toString().split("");
        for (int i = 0;i < dw.length - 1;i++) {
            for (int j = 0; j < dw.length - 1 - i; j++) {
                String temp = dw[j];
                dw[j] = dw[j + 1];
                dw[j + 1] = temp;
            }
        }

        StringBuffer str = new StringBuffer();
        int a = money.length;
        for (int i = 0; i < money.length; i++) {
            str.append(money[i]);
            str.append(dw[--a]);
        }

        return str.toString();
    }

}
